<?php
class send {
	var $happen_time=false;
	var $class_info=false;
	function __construct() {
		global $user,$game_id,$server_id;
		$this->happen_time=get_date_time();
		$class_id=$_GET['class_id'];
		$sql="select * from question_class where class_id='$class_id' and parent_id>0 and game_id='$game_id'";
		$row=sql_get_one($sql,__LINE__);
		if(!$row) js_alert('没有找到该分类，返回',true);
		$this->class_info['id']=$row['class_id'];
		$this->class_info['list_title']=$row['title'];
		$parent_id=$row['parent_id'];
		$sql="select * from question_class where class_id='$parent_id' and game_id='$game_id'";
		$row=sql_get_one($sql,__LINE__);
		$this->class_info['title']=$row['title'];
		$this->post();
	}
	function post() {
		global $user,$game_id,$server_id;
		if(!isset($_POST['class_id'])) return false;
		$this->check_send();
		$title=sql_filter($_POST['title']);
		$content=sql_filter($_POST['content']);
		
		//屏蔽词汇
		$title=filter_str($title);
		$content=filter_str($content);
		//end
		
		$class_id=$this->class_info['id'];
		$ask_time=get_date_time();
		$happen_time=sql_filter($_POST['time']);
		$user_id=$user->id;
		$sql="insert into question_info (title,content,ask_time,happen_time,user_id,class_id,game_id,server_id)
		values ('$title','$content','$ask_time','$happen_time','$user_id','$class_id','$game_id','$server_id')";
		if($res=run_sql($sql,__LINE__)) {
			$sql="select count(*) from question_info where user_id=$user_id";
			$row=sql_get_one($sql,__LINE__);
			$ques_count=$row['count(*)'];
			$sql="update user_info set ques_count=$ques_count where id=$user_id";
			sql_run($sql,__LINE__);
			$sql="select id from question_info where ask_time='$ask_time' and title='$title' and content='$content' and user_id='$user_id' and class_id='$class_id'";
			if($row=sql_get_one($sql,__LINE__)) {
				$pic1='';$pic2='';$pic3='';
				$id=$row['id'];
				if($_FILES['pic1']['error']==0) {
					$pic=addslashes(file_get_contents($_FILES['pic1']['tmp_name']));
					$sql="insert into question_pic (question_id,pic) values ('$id','$pic')";
					sql_run($sql,__LINE__);
				}
				if($_FILES['pic2']['error']==0) {
					$pic=addslashes(file_get_contents($_FILES['pic2']['tmp_name']));
					$sql="insert into question_pic (question_id,pic) values ('$id','$pic')";
					sql_run($sql,__LINE__);
				}
				if($_FILES['pic3']['error']==0) {
					$pic=addslashes(file_get_contents($_FILES['pic3']['tmp_name']));
					$sql="insert into question_pic (question_id,pic) values ('$id','$pic')";
					sql_run($sql,__LINE__);
				}
				header('location:'.'ques_detail.php?id='.$id);
				//js_alert('提交成功，您的问题，我们将尽快处理',true,'ques_detail.php?id='.$id);
			}
		}
	}
	function check_send(){
		global $user,$game_id,$server_id;
		
		if($user->nick_name == '牛肉熟了') js_alert('您因为违反规则被屏蔽了!',true);
		
		if($_POST['title'] == '') js_alert('请输入标题',true);
		if($_POST['content'] == '') js_alert('请输入内容',true);
		if($_POST['time'] == '') js_alert('请输入日期',true);
		$this->check_upload('1');
		$this->check_upload('2');
		$this->check_upload('3');

		//检查是否重名
		$title=$_POST['title'];
		$content=$_POST['content'];
		$user_id=$user->id;
		if(strlen($title)>150) js_alert('标题超过字数限制，最多只能提交120个汉字',true);
		if(strlen($content)>=2000) js_alert('内容超过字数限制，最多只能提交1000个汉字',true);
		$sql="select id from question_info where (title='$title' or content='$content') and user_id=$user_id and game_id=$game_id and server_id=$server_id ";
		if(sql_get_one($sql,__LINE__)) js_alert('这个问题您已经提交过了',true);
	}
	function check_upload($file) {
		$file_name=$_FILES['pic'.$file]['name'];
		if($file_name=='') return;
		$error=$_FILES['pic'.$file]['error'];
		$type=strtolower(substr($file_name,-3));
		
		$msg="上传的 图片".$file.' 出错，原因： \r\n\r\n';
		
		if($type!='gif' && $type!='png' && $type!='jpg' && $type!='bmp') js_alert($msg.'您上传的图片必须是 gif png jpg 类型的图片文件',true);
		if($_FILES['pic'.$file]['size']>102400) js_alert($msg.'图片超过大小请限制在 100K 以内',true);
		if($error==0) return;
		if($error==1 or $error==2) js_alert($msg.'图片超过大小请限制在 100K 以内',true);		
		if($error==3) js_alert($msg.'图片未上传完整，请重新提交',true);		
	}
}
?>